/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.compile;

import com.inspur.edp.sgf.api.entity.SgMetadata;

/**
 * 功能描述: @ClassName: EapiClass @Author: Fynn Qi @Date: 2020/9/17 14:06 @Version: V1.0
 */
public class EapiClass {

    /**
     * 生成Eapi类
     *
     * @param eapi      Eapi元数据实体
     * @param className 类名称
     * @return
     */
    public static String getClass(SgMetadata eapi, String className) {

        StringBuilder sb = new StringBuilder();

        // 添加类注解
        sb.append(EapiClassAnnotation.getEapiClassAnnotation(eapi));

        // 构建类结构
        sb.append(String.format("public class %s{", getExtensionClassName(eapi, className))).append(System.lineSeparator());
        sb.append(String.format("%s", getConstructor(eapi, className))).append(System.lineSeparator());
        sb.append(String.format("%s", getMethods(eapi))).append(System.lineSeparator());
        sb.append("}").append(System.lineSeparator());

        return sb.toString();
    }

    /**
     * 构建类名，可能存在继承的类
     *
     * @param eapi
     * @param className
     * @return
     */
    private static String getExtensionClassName(SgMetadata eapi, String className) {
        StringBuilder sb = new StringBuilder();
        if (EapiExtension.hasBaseClass(eapi)) {
            sb.append(String.format("%s extends %s", className, EapiExtension.getBaseClass(eapi)));
        } else {
            sb.append(className);
        }
        return sb.toString();
    }

    private static String getMethods(SgMetadata eapi) {
        return EapiMethod.getMethods(eapi);
    }

    private static String getConstructor(SgMetadata eapi, String className) {
        StringBuilder sb = new StringBuilder();
        if (EapiExtension.hasConstructorImpl(eapi)) {
            sb.append(EapiExtension.getConstructor(eapi, className));
        }
        return sb.toString();
    }
}
